package com.scpii.universal.dispatch.tools;

import java.io.File;
import java.io.IOException;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.HttpStatus;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.EntityUtils;

import com.scpii.universal.concurrent.Callback;
import com.scpii.universal.exception.DownloadException;

public class AndroidPicDownloadHandler extends PicDownloadHandler implements
		Callback {

	@Override
	public void downloadPic(String picFileDir, String fileName, String url)
			throws ClientProtocolException, IOException, DownloadException,
			Exception {
		HttpClient client = new DefaultHttpClient();
		HttpGet get = new HttpGet(url);
		HttpResponse response = null;
		try {
			response = client.execute(get);
		} catch (Exception e) {
			throw new DownloadException("请求URL资源出错，请查看url地址：" + url);
		}
		byte[] resData;
		if (HttpStatus.SC_OK == response.getStatusLine().getStatusCode()) {
			HttpEntity entity = response.getEntity();
			if (entity != null) {
				String filePath = picFileDir + File.separator + fileName;
				resData = EntityUtils.toByteArray(entity);
				saveNomarlSizePic(filePath, resData, this, null);
			}
		}

	}

	@Override
	public boolean handleMessage(Object... data) {
		return false;
	}

	@Override
	public boolean handleMessage(int action, Object... data) {
		// TODO Auto-generated method stub
		return false;
	}

}